package com.bookpavilion.order.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.bookpavilion.order.dao.OrderItemDao;
import com.bookpavilion.order.pojo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.bookpavilion.order.dao.OrderDao;
import com.bookpavilion.order.pojo.Order;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class OrderService {

	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private IdWorker idWorker;

	@Autowired
	private OrderItemDao orderItemDao;


	public void updateShippingMsg(String id, String shipping_name, String shipping_code) {
		orderDao.updateShippingMsg(id,shipping_name,shipping_code);
	}

	public void updateUpdateTimeById(String id){
		orderDao.updateUpdateTimeById(id,new Date());
	}

	

	public void updatePaymentTimeById(String id ){
		orderDao.updatePaymentTimeById(id,new Date());
	}

	

	public void updateEndTimeById(String id){
		orderDao.updateEndTimeById(id,new Date());
	}

	

	public void updateCloseTimeById(String id){
		orderDao.updateCloseTimeById(id,new Date());
	}

	public void updateConsignTimeById(String id){
		orderDao.updateConsignTimeById(id,new Date());
	}



	public void updateStatusById(String id,Integer status){
		orderDao.updateStatusById(id,status);
	}
	
	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Order> findAll() {
		return orderDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Order> findSearch(Map whereMap, int page, int size) {
		Specification<Order> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return orderDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Order> findSearch(Map whereMap) {
		Specification<Order> specification = createSpecification(whereMap);
		return orderDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Order findById(String id) {
		return orderDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param order
	 */
	public void add(Order order) {
		String orderId = idWorker.nextId() + "";
		order.setId(orderId);
		order.setStatus(1);
		order.setCreate_time(new Date());
		order.setUpdate_time(new Date());
		for (OrderItem orderItem : order.getOrder_item()) {
			orderItem.setId(idWorker.nextId()+"");
			orderItem.setOrder_id(orderId);
			orderItemDao.save(orderItem);
		}
		orderDao.save(order);
	}

	/**
	 * 修改
	 * @param order
	 */
	public void update(Order order) {
		orderDao.save(order);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		orderDao.updateStatusById(id,0);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Order> createSpecification(Map searchMap) {

		return new Specification<Order>() {

			@Override
			public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 订单id
                if (searchMap.get("order_id")!=null && !"".equals(searchMap.get("order_id"))) {
                	predicateList.add(cb.like(root.get("order_id").as(String.class), "%"+(String)searchMap.get("order_id")+"%"));
                }
                // 物流公司名称
                if (searchMap.get("shipping_name")!=null && !"".equals(searchMap.get("shipping_name"))) {
                	predicateList.add(cb.like(root.get("shipping_name").as(String.class), "%"+(String)searchMap.get("shipping_name")+"%"));
                }
                // 物流单号
                if (searchMap.get("shipping_code")!=null && !"".equals(searchMap.get("shipping_code"))) {
                	predicateList.add(cb.like(root.get("shipping_code").as(String.class), "%"+(String)searchMap.get("shipping_code")+"%"));
                }
                // 用户id ( 买家id )
                if (searchMap.get("user_id")!=null && !"".equals(searchMap.get("user_id"))) {
                	predicateList.add(cb.like(root.get("user_id").as(String.class), "%"+(String)searchMap.get("user_id")+"%"));
                }
                // 买家留言
                if (searchMap.get("buyer_message")!=null && !"".equals(searchMap.get("buyer_message"))) {
                	predicateList.add(cb.like(root.get("buyer_message").as(String.class), "%"+(String)searchMap.get("buyer_message")+"%"));
                }
                // 买家昵称
                if (searchMap.get("buyer_nick")!=null && !"".equals(searchMap.get("buyer_nick"))) {
                	predicateList.add(cb.like(root.get("buyer_nick").as(String.class), "%"+(String)searchMap.get("buyer_nick")+"%"));
                }
                // 买家是否评价
                if (searchMap.get("buyer_rate")!=null && !"".equals(searchMap.get("buyer_rate"))) {
                	predicateList.add(cb.like(root.get("buyer_rate").as(String.class), "%"+(String)searchMap.get("buyer_rate")+"%"));
                }
                // 收货人地址
                if (searchMap.get("receiver_address")!=null && !"".equals(searchMap.get("receiver_address"))) {
                	predicateList.add(cb.like(root.get("receiver_address").as(String.class), "%"+(String)searchMap.get("receiver_address")+"%"));
                }
                // 收货人手机
                if (searchMap.get("receiver_phone")!=null && !"".equals(searchMap.get("receiver_phone"))) {
                	predicateList.add(cb.like(root.get("receiver_phone").as(String.class), "%"+(String)searchMap.get("receiver_phone")+"%"));
                }
                // 邮政编码
                if (searchMap.get("receiver_zip_code")!=null && !"".equals(searchMap.get("receiver_zip_code"))) {
                	predicateList.add(cb.like(root.get("receiver_zip_code").as(String.class), "%"+(String)searchMap.get("receiver_zip_code")+"%"));
                }
                // 收货人姓名
                if (searchMap.get("receiver_name")!=null && !"".equals(searchMap.get("receiver_name"))) {
                	predicateList.add(cb.like(root.get("receiver_name").as(String.class), "%"+(String)searchMap.get("receiver_name")+"%"));
                }
                // 发票类型
                if (searchMap.get("invoice_type")!=null && !"".equals(searchMap.get("invoice_type"))) {
                	predicateList.add(cb.like(root.get("invoice_type").as(String.class), "%"+(String)searchMap.get("invoice_type")+"%"));
                }
                // 商家id
                if (searchMap.get("seller_id")!=null && !"".equals(searchMap.get("seller_id"))) {
                	predicateList.add(cb.like(root.get("seller_id").as(String.class), "%"+(String)searchMap.get("seller_id")+"%"));
                }
                // 商家名称
                if (searchMap.get("seller_name")!=null && !"".equals(searchMap.get("seller_name"))) {
                	predicateList.add(cb.like(root.get("seller_name").as(String.class), "%"+(String)searchMap.get("seller_name")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}


}
